package com.example.administrator.circle;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.graphics.Paint;
import android.graphics.Color;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.WindowManager;

import java.util.ArrayList;

public class DrawView extends View {
    //得到屏幕大小
    private DisplayMetrics dm = getResources().getDisplayMetrics();
    private int width = dm.widthPixels;
    private int heitht = dm.heightPixels;
    //下一点之坐标，半径
    private float currentX = 500;
    private float currentY = 500;
    private int r = 15;
    //点击坐标
    private float touchX;
    private float touchY;
    //触电与wheel点之距离，x，y，distance
    private float dis;
    private float disX;
    private float disY;
    //画笔
    private Paint P = new Paint();
    //速度，以及相应的x，y速度
    private int v = 12;
    private float vX;
    private float vY;
    //边界
    private float border = 10;
    private float Mixdis = 0;
    //格子宽度
    private int table = 100;
    //方向盘中心
    private float BothWheelX = 200;
    private float BothWheelY = 200;

    private float InnerWheelX = BothWheelX;
    private float InnerWheelY = BothWheelY;

    private float OuterWheelX = BothWheelX;
    private float OuterWheelY = BothWheelY;

    //方向盘半径,距离
    private float outerR = 150;
    private float innerR = 80;
    private float Rrdistance = outerR - innerR;


    //新建蛇阵
    private Paint P2 = new Paint();
    ArrayList<Point> SList = new ArrayList<Point>();

    private float foodX = width / 3;
    private float foodY = heitht / 3;
    private float foodR = 15;

    //构造函数
    public DrawView(Context context, AttributeSet set) {
        super(context, set);
    }

    //绘图
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.rgb(195, 255, 231));

        //画格子,蛇,食物，方向盘
        drawTable(canvas, table);
        drawSnake(canvas);
        drawFood(canvas);
        drawWheel(canvas);
    }

    //根据点击移动
    private void chXYByTouch() {
        currentX = currentX + vX;
        currentY = currentY + vY;

    }

    //检测是否到达边界
    private boolean isArriveBorder() {
        if (currentX < r || currentX > width - r) {
            return true;
        }
        if (currentY < r || currentY >= heitht - r) {
            return true;
        }
        return false;
    }

    //检测是否为空，空则添加
    private void SnakeBorn() {
        if (SList.size() == 0) {
            SList.add(new Point(currentX, currentY));
        }

    }

    //检测是否越界，改变之
    private void checkXY() {
        if (currentX < 0) {
            currentX = width;
        } else if (currentX > width) {
            currentX = 0;
        }
        if (currentY < 0) {
            currentY = heitht;
        } else if (currentY > heitht) {
            currentY = 0;
        }
    }

    //蛇移动
    private void SMove() {
        if (SList.size() >= 2) {
            for (int i = SList.size() - 1; i >= 1; i--) {
                SList.set(i, SList.get(i - 1));
            }
        }
        Point point = new Point(currentX, currentY);
        SList.set(0, point);
    }

    //蛇边长
    private void SAdd() {
        SList.add(SList.get(SList.size() - 1));
        SMove();
    }

    //改变实物位置
    private void foodXYch() {
        foodX = (float) (Math.random()) * width;
        foodY = (float) (Math.random()) * heitht;
        if (foodX >= width || foodY >= heitht || foodX <= 0 || foodY <= 60) {
            foodXYch();
        }
        if (foodX <= table * 4 && foodY <= table * 4) {
            foodXYch();
        }
    }

    //检测是否相遇
    private boolean isMeet() {
        for (int i = 0; i < SList.size(); i++) {
            float xtemp = SList.get(i).getpX();
            float ytemp = SList.get(i).getpY();

            float disx = (xtemp - foodX) * (xtemp - foodX);
            float disy = (ytemp - foodY) * (ytemp - foodY);
            float disr = (r + foodR) * (r + foodR);
            if ((disx + disy) < disr) {
                return true;
            }
        }
        return false;
    }

    //相遇都改变
    private void chBoth() {
        if (isMeet() == true) {
            SAdd();
            foodXYch();
        } else if (isMeet() == false) {
            SMove();
        }

    }

    //点击屏幕
    public boolean onTouchEvent(MotionEvent event) {
        disX = event.getX() - OuterWheelX;
        disY = event.getY() - OuterWheelY;
        dis = (float) Math.sqrt(disX * disX + disY * disY);
        vX = disX / dis * v;
        vY = disY / dis * v;
        //检测是否在框外，框内，随同点击，框外，算法定制
        if (dis <= Rrdistance) {
            InnerWheelX = event.getX();
            InnerWheelY = event.getY();
        } else {
            InnerWheelX = BothWheelX + Rrdistance * disX / dis;
            InnerWheelY = BothWheelY + Rrdistance * disY / dis;
        }
        return true;

    }

    //画格子，间隔为table
    private void drawTable(Canvas canvas, int table) {
        P.setColor(Color.BLACK);
        P.setStrokeWidth(2);
        for (int i = table; i < width; i = i + table) {
            canvas.drawLine(i, 0, i, heitht, P);
        }
        for (int i = table; i < heitht; i = i + table) {
            canvas.drawLine(0, i, width, i, P);
        }
    }

    private void drawSnake(Canvas canvas) {
        SnakeBorn();
        for (int i = 0; i < SList.size(); i++) {
            float tempx2 = SList.get(i).getpX();
            float tempy2 = SList.get(i).getpY();
            switch (i % 4) {
                case 0:
                    P.setColor(Color.GREEN);
                    break;
                case 1:
                    P.setColor(Color.BLACK);
                    break;
                case 2:
                    P.setColor(Color.WHITE);
                    break;
                case 3:
                    P.setColor(Color.RED);
                    break;
            }
            canvas.drawCircle(tempx2, tempy2, r, P);
        }
        ;
    }

    private void drawFood(Canvas canvas) {
        canvas.drawCircle(foodX, foodY, foodR, P);
    }

    private void drawWheel(Canvas canvas) {
        P.setColor(Color.BLACK);
        P.setAlpha(200);
        canvas.drawCircle(OuterWheelX, OuterWheelY, outerR, P);
        P.setColor(Color.WHITE);
        P.setAlpha(250);
        canvas.drawCircle(InnerWheelX, InnerWheelY, innerR, P);
        P.setColor(Color.BLACK);
        canvas.drawCircle(BothWheelX, BothWheelY, 25, P);
        canvas.drawCircle(InnerWheelX, InnerWheelY, 10, P);
    }

    //根据方向盘控制移动
    public void ControlByWheel() {
        if (isArriveBorder() == false) {
            chXYByTouch();
        }
        invalidate();
    }

    //方向盘控制蛇，贪吃食物
    public void Control() {
        SnakeBorn();
        checkXY();
        chXYByTouch();
        chBoth();
        invalidate();
    }


}
